package com.iximo.openAPI.impl;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;


import com.iximo.domain.Book;
import com.iximo.domain.Books;
import com.iximo.domain.Category;
import com.iximo.domain.Categorys;
import com.iximo.domain.Chapter;
import com.iximo.domain.Chapters;
import com.iximo.domain.Comment;
import com.iximo.domain.Comments;
import com.iximo.domain.Reply;
import com.iximo.domain.Results;
import com.iximo.openAPI.IximoOpenAPIGet;
import com.iximo.util.BaseUrl;
import com.iximo.util.InternetService;
import com.thoughtworks.xstream.XStream;


public class IximoOpenAPIGetImpl implements IximoOpenAPIGet{

	//require login
	public String downloadBook(int bookId) throws Exception {
		String uri = BaseUrl.getBookdownloadUrl + "?book_id=" + bookId;
		InputStream inStream = InternetService.get(uri);
		String downloadUrl = InternetService.readStream(inStream);
		return downloadUrl;
	}

	public Book getBookInfo(int bookId) throws Exception {
		String uri = BaseUrl.getBookInfoUrl + "?book_id=" + bookId;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("book", Book.class);
		Book book = (Book)xStream.fromXML(readInput);
		return book;
	}
	
	public Book getBookInfoData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("book", Book.class);
		Book book = (Book)xStream.fromXML(readInput);
		return book;
	}
	
	public List<Book> getBookByNum(int bookId, int num) throws Exception {
		String uri = BaseUrl.getBookOtherPeopleListUrl + "?book_id=" + bookId + "&num=" + num; 
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
//		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	public List<Book> getBookByNumData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}

	public List<Book> getBooksByCategary(int categoryId, String sort, int page,
			int pageSize) throws Exception {
		String uri = "";
//		String uri = BaseUrl.getBookCategorySortListUrl + 
//					"?cate_id=" + categoryId + "&sort=" + sort + "&page=" + page + "&pagesize=" + pageSize;			
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		//doesn't need to display it's root tag
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		List<Book> listBook = books.getBooks();
		return listBook;
	}
	
	public List<Book> getBooksByCategaryData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		//doesn't need to display it's root tag
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		List<Book> listBook = books.getBooks();
		return listBook;
	}

	public List<Category> getAllCategory(int parentId) throws Exception {
		String uri = BaseUrl.getBookCategorysUrl + "?parent_id=" + parentId;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("categorys", Categorys.class);
		xStream.alias("category", Category.class);
		xStream.addImplicitCollection(Categorys.class, "categorys");
		Categorys categorys = (Categorys)xStream.fromXML(readInput);
		return categorys.getCategorys();
	}
	
	public List<Category> getAllCategoryData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("categorys", Categorys.class);
		xStream.alias("category", Category.class);
		xStream.addImplicitCollection(Categorys.class, "categorys");
		Categorys categorys = (Categorys)xStream.fromXML(readInput);
		return categorys.getCategorys();
	}
	/**
	 * InputStream 转换成 InputStreamReader
	 */
	private InputStreamReader toReadInput(String uri) throws Exception {
		InputStream inStream = InternetService.get(uri);
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		return readInput;
	}
	
	public List<Book> getBookListByType(String type, int page, int pageSize)
			throws Exception {
		String uri = BaseUrl.getBookSortListUrl + "?type=" + type + "&page=" + page + "&pagesize=" + pageSize;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	
	public List<Book> getBookListByTypeData(InputStream inStream)
			throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}

	public List<Chapter> getChapter(int bookId) throws Exception {
		String uri = BaseUrl.getBookChaptersUrl + "?book_id=" + bookId;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("chapters", Chapters.class);
		xStream.alias("chapter", Chapter.class);
		xStream.addImplicitCollection(Chapters.class, "Chapters");
		Chapters chapters = (Chapters)xStream.fromXML(readInput);
		return chapters.getChapters();
	}
	
	public List<Chapter> getChapterData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("chapters", Chapters.class);
		xStream.alias("chapter", Chapter.class);
		xStream.addImplicitCollection(Chapters.class, "Chapters");
		Chapters chapters = (Chapters)xStream.fromXML(readInput);
		return chapters.getChapters();
	}

	public String getChapterContent(int chapterId) throws Exception {
		String uri = BaseUrl.getBookChapterContentUrl + "?chapter_id=" + chapterId;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("chapter", Chapter.class);
		Chapter chapter = (Chapter)xStream.fromXML(readInput);
		return chapter.getContent();
	}
	
	public String getChapterContentData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("chapter", Chapter.class);
		Chapter chapter = (Chapter)xStream.fromXML(readInput);
		return chapter.getContent();
	}
	//sth... 
	public Comments getComment(int bookId, int page, int pagesize) throws Exception {
		String uri = BaseUrl.getBookCommentsUrl + "?book_id=" + bookId + "&page=" + page + "&pagesize=" + pagesize;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("comments", Comments.class);
		xStream.alias("comment", Comment.class);
		xStream.alias("reply", Reply.class);
		xStream.addImplicitCollection(Comments.class, "comments");
		xStream.addImplicitCollection(Comment.class, "replys");
		Comments  comments = (Comments)xStream.fromXML(readInput);
		return comments;
	}
	
	public Comments getCommentData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("comments", Comments.class);
		xStream.alias("comment", Comment.class);
		xStream.alias("reply", Reply.class);
		xStream.addImplicitCollection(Comments.class, "comments");
		xStream.addImplicitCollection(Comment.class, "replys");
		Comments  comments = (Comments)xStream.fromXML(readInput);
		return comments;
	}

	public Books searchBook(String keyWords) throws Exception {
		String uri = BaseUrl.searchBookUrl + "?search_val=" + keyWords;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books;
	}
	
	public List<Book> searchBookData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	//no : (require login else return 2)
	public List<Book> getBookMark(int page, int pageSize) throws Exception {
		String uri = BaseUrl.myBookMarkUrl + "?page=" + page + "&pagesize=" + pageSize;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	
	public List<Book> getBookMarkData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	//require login
	public int addBookMark(int bookId, int chapterId) throws Exception {
		String uri = BaseUrl.addBookMarUrl + "?book_id=" + bookId + "&chapter_id=" + chapterId;
		InputStream inStream = InternetService.get(uri);
		String ret = InternetService.readStream(inStream);
		return Integer.valueOf(ret);
	}
	
	//require login
	public List<Book> getFavoriteBook(int page, int pageSize) throws Exception {
		String uri = BaseUrl.myFavoriteUrl + "?page=" + page + "&pagesize=" + pageSize;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	
	public List<Book> getFavoriteBookData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		return books.getBooks();
	}
	//require login
	public int addFavoriteBook(int bookId) throws Exception {
		String uri = BaseUrl.addFavoriteUrl + "?book_id=" + bookId;
		InputStream inStream = InternetService.get(uri);
		String ret = InternetService.readStream(inStream);
		return Integer.valueOf(ret);
	}
	//require login
	public int deleteFavorite(int favoriteId) throws Exception {
		String uri = BaseUrl.deleteFavariteUrl + "?f_id=" + favoriteId;
		InputStream inStream = InternetService.get(uri);
		String ret = InternetService.readStream(inStream);
		return Integer.valueOf(ret);
	}
	//require login
	public int addVote(int bookId) throws Exception {
		String uri = BaseUrl.addVoteUrl + "?book_id=" + bookId;
		InputStream inStream = InternetService.get(uri);
		String ret = InternetService.readStream(inStream);
		return Integer.valueOf(ret);
	}
	//require login
	public List<Books> getDownloadList() throws Exception {
		String uri = BaseUrl.myDownloadListUrl;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		System.out.println(books.getDownloadnum());
		return books.getBooks();
	}
	public List<Books> getDownloadListData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		xStream.addImplicitCollection(Books.class, "books");
		xStream.addImplicitCollection(Book.class, "categoryName");
		Books books = (Books)xStream.fromXML(readInput);
		System.out.println(books.getDownloadnum());
		return books.getBooks();
	}

	public Books getHotBoooks(String type, int recomment, int newbook)
			throws Exception {
		String uri = BaseUrl.getHotBookListUrl + "?type=" + type;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		Books books = (Books)xStream.fromXML(readInput);
		return books;
	}
	
	public Books getHotBoooksData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("books", Books.class);
		xStream.alias("book", Book.class);
		Books books = (Books)xStream.fromXML(readInput);
		return books;
	}
	//require login
	public int randBook(int score, int bookId) throws Exception {
		String uri = BaseUrl.addRatingUrl + "?book_id=" + bookId + "&score=" + score;
		InputStream inStream = InternetService.get(uri);
		String ret = InternetService.readStream(inStream);
		return Integer.valueOf(ret);
	}
	//require login
	public String getMemberBalance(String key) throws Exception {
		String uri = BaseUrl.myBalanceUrl + "?key=b2b0acc3a188e051fd461c36dfccd897"; 
		InputStream inStream = InternetService.get(uri);
		return InternetService.readStream(inStream);
	}
	
	public List<Chapter> getBuyChapterList(int bookId, String key) throws Exception {
//		String keyMD5 = new MD5Encoder().encode("smartphone_api_payment_iximo_cc".getBytes());
//		System.out.println(keyMD5);
		String uri = BaseUrl.getBookVipChapterUrl + "?book_id=" + bookId + "&key=" + "b2b0acc3a188e051fd461c36dfccd897" ;
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("chapters", Chapters.class);
		xStream.alias("chapter", Chapter.class);
		xStream.addImplicitCollection(Chapters.class, "Chapters");
		Chapters chapters = (Chapters)xStream.fromXML(readInput);
		return chapters.getChapters();
	}
	
	public List<Chapter> getBuyChapterListData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("chapters", Chapters.class);
		xStream.alias("chapter", Chapter.class);
		xStream.addImplicitCollection(Chapters.class, "Chapters");
		Chapters chapters = (Chapters)xStream.fromXML(readInput);
		return chapters.getChapters();
	}
	//require login
	public List<Chapter> payChapterList(int bookId, String key, String...chapterIds)
			throws Exception {
		StringBuilder chaIds = new StringBuilder();
		for(String chaId : chapterIds){
			chaIds.append(chaId).append(',');
		}
		chaIds.deleteCharAt(chaIds.length()-1);
		String uri = BaseUrl.payBookUrl + "?book_id=" + bookId + "&cha_ids=" + chaIds.toString() + "&key=b2b0acc3a188e051fd461c36dfccd897";
		InputStreamReader readInput = toReadInput(uri);
		XStream xStream = new XStream();
		xStream.alias("results", Results.class);
		xStream.alias("chapter", Chapter.class);
		Results results = (Results)xStream.fromXML(readInput);
		System.out.println(results.getResult());
		return results.getChapters();
	}
	public List<Chapter> payChapterListData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("results", Results.class);
		xStream.alias("chapter", Chapter.class);
		Results results = (Results)xStream.fromXML(readInput);
		System.out.println(results.getResult());
		return results.getChapters();
	}

	public String getNotic() throws Exception {
		String uri = BaseUrl.getNoticeUrl;
		InputStream inStream = InternetService.get(uri);
		return InternetService.readStream(inStream);
	}

	public String getCurrentIphoneVersion() throws Exception {
		String uri = BaseUrl.getIphoneVersionUrl;
		InputStream inStream = InternetService.get(uri);
		return InternetService.readStream(inStream);
	}
	
}
